package com.hfuu.controller.user;

import com.hfuu.model.ResponseEntity;
import com.hfuu.model.ResponseEntityBuilder;
import com.hfuu.model.po.TbUser;
import com.hfuu.service.user.UserService;
import com.hfuu.service.user.request.ManSysUserRequest;
import com.hfuu.service.user.request.UserLoginRequest;
import com.hfuu.service.user.request.UserRequest;
import com.hfuu.service.user.response.UserLoginResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.MediaType;

//todo 登录session拦截
@Api(tags="用户管理类")
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("用户注册更新")
    @RequestMapping(value = "/insertOrUpdateUser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON)
    @ResponseBody
    public ResponseEntity<TbUser> insertOrUpdateUser(@RequestBody UserRequest request) throws Exception{
        return ResponseEntityBuilder.success(userService.insertOrUpdateUser(request));
    }

    @ApiOperation("用户登录")
    @RequestMapping(value = "/userLogin", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON)
    @ResponseBody
    public ResponseEntity<UserLoginResponse> sysLogin(HttpServletRequest request,
                                                          @RequestBody UserLoginRequest sysLoginRequest)
            throws Exception {
        return ResponseEntityBuilder
                .success(this.userService.sysLogin(request, sysLoginRequest));
    }
    @ApiOperation("注销用户")
    @RequestMapping(value = "/userDisable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON)
    @ResponseBody
    public ResponseEntity<Object> userDisable(HttpServletRequest request,
                                                      @RequestBody UserLoginRequest sysLoginRequest)
            throws Exception {
        return ResponseEntityBuilder
                .success(this.userService.userDisable(request, sysLoginRequest));
    }

    @ApiOperation("修改用户密码")
    @RequestMapping(value = "/sysUserPass", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON)
    @ResponseBody
    public ResponseEntity<Object> sysUserPass(@RequestBody ManSysUserRequest sysUserRequest)
            throws Exception {
        this.userService.updateSysUserPasswd(sysUserRequest);
        return ResponseEntityBuilder.success();
    }

    @ApiModelProperty("获取验证码图片")
    @RequestMapping(value = "/getCheckImage", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON)
    @ResponseBody
    public ResponseEntity<Object> getCheckImage(HttpServletRequest request,
                                                 HttpServletResponse response) {
        return ResponseEntityBuilder.success(userService.getCheckImage(request, response));
    }

    @ApiModelProperty("判断验证码是否正确")
    @RequestMapping(value = "/judgeCheckCode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON)
    @ResponseBody
    public ResponseEntity<Object>  judgeCheckCode(HttpServletRequest request,
                                                  @RequestBody UserLoginRequest userLoginRequest) {

        return ResponseEntityBuilder.success(userService.judgeCheckCode(request, userLoginRequest));

    }
    @ApiModelProperty("查询用户名，是否已存在")
    @RequestMapping(value = "/judgeUserName", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON)
    @ResponseBody
    public ResponseEntity<Object>  judgeUserName(HttpServletRequest request,
                                                  @RequestBody UserLoginRequest userLoginRequest) {

        return ResponseEntityBuilder.success(userService.judgeUserName(request, userLoginRequest));

    }

    @ApiModelProperty("根据用户Id，查询用户")
    @RequestMapping(value = "/getUserById", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON)
    @ResponseBody
    public ResponseEntity<Object>  getUserById(HttpServletRequest request,
                                                 @RequestBody UserLoginRequest userLoginRequest) throws Exception {

        return ResponseEntityBuilder.success(userService.getUserById(request, userLoginRequest));

    }
}
